package com.dominator.serviceImpl;

import com.dominFramework.core.typewrap.Dto;
import com.dominFramework.core.utils.JsonUtils;
import com.dominFramework.core.utils.SystemUtils;
import com.dominator.service.ApiOtosaasService;
import com.dominator.utils.api.ApiMessage;
import com.dominator.utils.api.ApiMessageUtil;
import com.dominator.utils.api.ApiUtils;
import com.dominator.utils.encode.MD5;
import com.dominator.utils.exception.ApiException;
import com.dominator.utils.network.HttpClientUtils;
import com.dominator.utils.network.HttpPoster;
import com.dominator.utils.system.PropertiesLoader;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class ApiOtosaasServiceImpl implements ApiOtosaasService {

    public static PropertiesLoader propertiesLoader = new PropertiesLoader("sysconfig.properties");


    @Override
    public String validation(HttpServletRequest request) {
        String url=propertiesLoader.getProperty("oto_api_validation_url");
        log.info("接口调用" + request.getRequestURL() +"?"+ request.getQueryString());
        if (SystemUtils.isNotEmpty(request.getQueryString())) {
            url += "?" + request.getQueryString();
        }
        String res = null;
        try {
            res = HttpClientUtils.get(url);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return res;
    }

    @Override
    public ApiMessage jointLogin(Dto dto) throws ApiException {
        ApiUtils.checkParam(dto,"customerUserId,customerUserPhone,customerUserPhone");
        log.info("[联合登录参]：" + dto.toString());
        String otosaasLoginUrl=propertiesLoader.getProperty("OTOSAAS_LOGIN_URL");
        String joinLoginAppkey=propertiesLoader.getProperty("JOIN_LOGIN_APPKEY");
        String joinLoginAppSecret=propertiesLoader.getProperty("JOIN_LOGIN_APPSECRET");

        String OTOSAAS_LOGIN_URL = otosaasLoginUrl;
        String customerUserId = dto.getString("customerUserId");
        String customerUserPhone = dto.getString("customerUserPhone");
        String customerPayLoad = dto.getString("customerPayLoad");
        String timeStamp = (System.currentTimeMillis() / 1000) + "";

        //获取签名(参与签名的参数为：appkey,customerUserId, customerUserPhone, timestamp)
        Map<String, String> map = new HashMap<String, String>();
        map.put("appKey", joinLoginAppkey);
        map.put("customerUserId", customerUserId);
        map.put("customerUserPhone", customerUserPhone);
        map.put("customerPayLoad", customerPayLoad);
        map.put("timeStamp", timeStamp);
        map.put("appSecret", joinLoginAppSecret);
        log.info("[参与签名参数]：" + map);
        String sign = getSign4OtoSaaS(map);
        OTOSAAS_LOGIN_URL = OTOSAAS_LOGIN_URL.replace("APPKEY", joinLoginAppkey)
                .replace("CUSTOMERUSERID", customerUserId).replace("CUSTOMERUSERPHONE", customerUserPhone)
                .replace("TIMESTAMP", timeStamp).replace("SIGN", sign)
                .replace("CUSTOMERPAYLOAD", customerPayLoad);
        log.info("[返回结果]" + OTOSAAS_LOGIN_URL);
        return ApiMessageUtil.success(OTOSAAS_LOGIN_URL);
    }


    /**
     * 加签
     *
     * @param map
     * @return
     * @throws NoSuchAlgorithmException
     */
    public String getSign4OtoSaaS(Map<String, String> map) {
        String strC = null;
        try {
            //第一步获取字符串A(customerUserId + customerUserPhone + timeStamp)
            String strA = map.get("customerUserId") + map.get("customerUserPhone") + map.get("timeStamp");
            //第二步获取字符串B(appKey + 字符串A + appSecret)
            String strB = map.get("appKey") + strA + map.get("appSecret");
            //第三步获取字符串C（对字符串B进行md5哈希）
            strC = MD5.encode(strB);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return strC;
    }

}
